import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_study_app/app/router/routers.dart';
import 'package:flutter_study_app/widget/my_app_bar.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

class ArchivesStatisticsPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => _ArchivesStatisticsPageState();
}

class _ArchivesStatisticsPageState extends State<ArchivesStatisticsPage> {
  bool _showToTopBtn = false;
  ScrollController _controller = ScrollController();

  @override
  void initState() {
    super.initState();
    _controller.addListener(() {
      if (_controller.offset < 500 && _showToTopBtn) {
        setState(() {
          _showToTopBtn = false;
        });
      } else if (_controller.offset >= 500 && _showToTopBtn == false) {
        setState(() {
          _showToTopBtn = true;
        });
      }
    });
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  _buildArchivesItemView(int index) {
    return Card(
      elevation: 3,
      margin: EdgeInsets.only(
          left: 12.w, right: 12.w, bottom: 12.w, top: index == 0 ? 10.w : 0),
      shape: RoundedRectangleBorder(
        borderRadius: BorderRadius.all(Radius.circular(8.w)),
      ),
      color: Colors.white,
      child: Container(
        child: Column(
          children: [
            SizedBox(
              height: 10.w,
            ),
            Container(
              alignment: Alignment.centerLeft,
              padding: EdgeInsets.symmetric(horizontal: 12.w),
              child: Text(
                '合同编号：87346785682398129074',
                style: TextStyle(fontSize: 14.sp, color: Color(0xFF666666)),
              ),
            ),
            Container(
              margin: EdgeInsets.symmetric(horizontal: 6.w, vertical: 8.w),
              height: 1.w,
              color: Color(0xFFD8D8D8),
            ),
            Container(
              padding: EdgeInsets.symmetric(horizontal: 12.w),
              child: Row(
                children: [
                  Text(
                    '张三',
                    style: TextStyle(fontSize: 14.sp, color: Color(0xFF333333)),
                  ),
                  Spacer(),
                  Text(
                    '￥2553.23',
                    style: TextStyle(
                        fontSize: 16.sp,
                        color: Color(0xFF333333),
                        fontWeight: FontWeight.bold),
                  )
                ],
              ),
            ),
            SizedBox(
              height: 3.w,
            ),
            Container(
              padding: EdgeInsets.symmetric(horizontal: 12.w),
              child: Row(
                children: [
                  Expanded(
                    child: Text(
                      '产品名称产品名称产品名称…',
                      maxLines: 1,
                      overflow: TextOverflow.ellipsis,
                      style:
                          TextStyle(fontSize: 14.sp, color: Color(0xFF333333)),
                    ),
                  ),
                  SizedBox(
                    width: 15.w,
                  ),
                  Text(
                    '2019-02-25',
                    style: TextStyle(
                        fontSize: 13.sp,
                        color: Color(0xFF6D7272),
                        fontWeight: FontWeight.bold),
                  ),
                ],
              ),
            ),
            SizedBox(
              height: 10.w,
            ),
          ],
        ),
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Stack(
        children: [
          Column(
            children: [
              Container(
                child: AspectRatio(
                  aspectRatio: 1080 / 455,
                  child: Stack(
                    children: [
                      Image.asset(
                        'assets/images/cs_stat_record_bg.png',
                        fit: BoxFit.fitWidth,
                      ),
                      Container(
                        margin: EdgeInsets.only(top: 65.w),
                        width: double.infinity,
                        child: Column(
                          mainAxisAlignment: MainAxisAlignment.center,
                          children: [
                            Text(
                              '0',
                              style: TextStyle(
                                  color: Colors.white,
                                  fontSize: 32.sp,
                                  fontWeight: FontWeight.w500),
                            ),
                            Text(
                              '本月需缴档案笔数',
                              style: TextStyle(
                                  color: Colors.white, fontSize: 15.sp),
                            )
                          ],
                        ),
                      )
                    ],
                  ),
                ),
              ),
              Expanded(
                  child: Container(
                child: MediaQuery.removePadding(
                  context: context,
                  removeTop: true,
                  child: ListView.builder(
                      controller: _controller,
                      itemCount: 50,
                      itemBuilder: (context, index) {
                        return InkWell(
                          onTap: () {
                            Routers.navigateTo(
                                context, Routers.archivesStatisticsDetailPage);
                          },
                          child: _buildArchivesItemView(index),
                        );
                      }),
                ),
              ))
            ],
          ),
          Positioned(
            top: 0,
            left: 0,
            right: 0,
            child: MyAppBar(
              backBgColor: Colors.white,
              bgColor: Colors.transparent,
              title: '档案管理',
              titleColor: Colors.white,
              style: SystemUiOverlayStyle.light,
            ),
          )
        ],
      ),
      floatingActionButton: !_showToTopBtn
          ? null
          : GestureDetector(
              child: Image.asset(
                'assets/images/cs_to_top.png',
                width: 60.w,
                height: 60.w,
              ),
              onTap: () {
                //返回到顶部时执行动画
                _controller.animateTo(
                  .0,
                  duration: Duration(milliseconds: 200),
                  curve: Curves.ease,
                );
              }),
    );
  }
}
